﻿using System;
using System.Collections.Generic;
using System.Text;
using SantaSLittleIrcHelper.Irc;
using System.Globalization;
using System.Threading;
using System.Drawing;


namespace SantaSLittleIrcHelper.Registration
{
    class CheckAvailabilityProcess : Process
    {

        public override void start()
        {
            this.feedbackLabel = (TLabel)Program.instance.GUI.Controls.Find("labelCheckAvailabilityFeedback", true)[0];
            if (!Program.instance.IRC.IsConnected)
            {
                this.invokeSetFeedbackMessage(Color.Orange, "connecting");
                Program.instance.connect();
                return;
            }
            this.invokeSetFeedbackMessage(Color.Orange, "processRunning");
            Settings s = Program.instance.settings;
            this.step = new StepCallback(this.nickservAnswer);
            Program.instance.consoleWrite("Starting process CheckAvailabilityProcess");
            Program.instance.IRC.WriteLine(Rfc2812.Privmsg("nickserv", "info " + s.nsUsername));
            this.triggerString = "NickServ";
        }

        public void nickservAnswer(String msg)
        {
            Settings s = Program.instance.settings;
            if (msg.Contains("isn't registered"))
            {

                Program.instance.consoleWrite("name is not registered; renaming now to check if it is currently used.");
                Program.instance.IRC.WriteLine(Rfc2812.Nick(s.nsUsername));
                this.step = new StepCallback(this.renamed);
                this.triggerString = Program.instance.nickname;
            }
            else if (msg.Contains(s.nsUsername + " is "))
            {
                Program.instance.consoleWrite("name is registered, cancelling process");
                this.invokeSetFeedbackMessage(Color.Red, "availabilityProcessUsernameAlreadyRegistered", s.nsUsername);
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameIsNotAvailable);
                Program.instance.killProcess();
            }
            else if (msg.Contains("This nick is awaiting an e-mail verification code before completing registration."))
            {
                Program.instance.consoleWrite("chosen username is registered, but not confirmed");
                this.invokeSetFeedbackMessage(Color.Red, "availabilityProcessUsernameAlreadyRegistered", s.nsUsername);
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameIsNotAvailable);
                Program.instance.killProcess();
            }
        }

        public void renamed(String msg)
        {
            Settings s = Program.instance.settings;
            if (msg.Contains("Nickname is already in use"))
            {
                Program.instance.consoleWrite(String.Format(CultureInfo.InvariantCulture, Languages.Instance.get("availabilityProcessUsernameUsedByGuest"), s.nsUsername));
                Program.instance.consoleWrite(Languages.Instance.get("availabilityProcessFinished"));
                this.invokeSetFeedbackMessage(Color.Red, "availabilityProcessUsernameUsedByGuest", s.nsUsername);
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameIsNotAvailable);
                Program.instance.killProcess();
            }
            else if (msg.Contains("NICK"))
            {
                Program.instance.consoleWrite(String.Format(CultureInfo.InvariantCulture, Languages.Instance.get("availabilityProcessUsernameCanBeUsed"), s.nsUsername));
                if (s.verboseMode) Program.instance.consoleWrite(String.Format(Languages.Instance.get("renamingTo"), Program.instance.nickname));
                Program.instance.IRC.WriteLine(Rfc2812.Nick(Program.instance.nickname));
                this.invokeSetFeedbackMessage(Color.Green, "availabilityProcessUsernameCanBeUsed", s.nsUsername);
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameIsAvailable);
                this.step = new StepCallback(this.renameBack);
                this.triggerString = "NICK";
            }

        }

        public void renameBack(String msg)
        {
            Settings s = Program.instance.settings;
            if (msg.Contains(Program.instance.nickname))
            {
                if (s.verboseMode)
                {
                    Program.instance.consoleWrite(String.Format(CultureInfo.InvariantCulture, Languages.Instance.get("availabilityProcessFinishedVerbose"), Program.instance.nickname));
                }
                else
                {
                    Program.instance.consoleWrite(Languages.Instance.get("availabilityProcessFinished"));
                }
                Program.instance.killProcess();

            }
        }

    }
}
